<?php


namespace app\msgapi\service;

use app\msgapi\model\OperateLog;

class HsoperateLogService extends BaseService
{
    protected $limit; //每页默认数量
    public function __construct(OperateLog $operateLog)
    {
        parent::__construct();
        $this->model = $operateLog;
        $this->limit = config('page.defaule_limit');
    }

    /**日志列表
     * @return array|\think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function getLists()
    {
        $data = input('param.');
        $adminlst = HsadminService::getAdminAccount(request()->admin_groupid,request()->adminid,true); //获取可见管理员账号
        if($data['receive_user'] && !in_array($data['receive_user'],$adminlst)) return error_json('暂无此账号权限');
        $where[] = ['ad_uid','=',$data['receive_user']?:request()->adminid];
        $where[] = ['type','=',$data['type']];
        $limit = $data['limit']?:$this->limit;
//        $list = $this->model->with('user')->withSearch('type', ['type'=>$data['type']])->order('add_time desc')->paginate($limit)->toArray();
        $list = $this->model->with('user')->where($where)->order('add_time desc')->paginate($limit)->toArray();
        return success_json('成功',$list);
    }

    /**日志
     * @param $type 1登录2修改参数3同步通讯录4查看消息
     * @param $ad_uid
     * @param $content
     * @param string $detail
     * @return OperateLog|\think\Model
     */
    public static function addLog($type,$ad_uid,$content,$detail='',$is_within = true)
    {
        if($is_within){
            if($ad_uid != 1){
                $add_time = date('Y-m-d H:i:s');
                $ip =$_SERVER['REMOTE_ADDR'];
                $data = compact('ad_uid','type','add_time','content','ip','detail');
                return OperateLog::create($data);
            }
        }else{
            if($ad_uid != 1){
                $add_time = date('Y-m-d H:i:s');
                $ip =$_SERVER['REMOTE_ADDR'];
                $data = compact('ad_uid','type','add_time','content','ip','detail');
                OperateLog::create($data);
                return success_json('成功');
            }else{
                return success_json('成功');
            }
        }

    }

}